package com.sense.kukoo.biz_library.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.biz.FollowContentBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean

class LibraryViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mPageIndex = 2
    private val mPageSize = 20

    private val mData = arrayListOf<IndexContentBean>()
    private val mTitleList = arrayListOf<UserSearchBean>()
    private var mCurrentSelectedUser: UserSearchBean? = null

    private var errorRetry: ()->Unit = {}

    val action_finishRefresh: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val action_network_error: MutableLiveData<Boolean> = MutableLiveData()
    val action_toChannelPage: MutableLiveData<UserSearchBean> = MutableLiveData()
    val data_enableLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val data_content: MutableLiveData<List<FollowContentBean>> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun showData(){
        val followContents = arrayListOf<FollowContentBean>()
        if (!mTitleList.isEmpty()) {
            followContents.add(FollowContentBean(type = FollowContentBean.TYPE_TITLE,
                                                 titleList = mTitleList,
                                                 currentSelectedUser = mCurrentSelectedUser
            ))
        }
        mData.forEach {
            followContents.add(FollowContentBean(type = FollowContentBean.TYPE_ITEM, data = it))
        }
        action_finishRefresh.postValue(true)
        action_finishLoadMore.postValue(true)
        data_content.postValue(followContents)
    }

    fun refreshFollowContents(){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            errorRetry = {refreshFollowContents()}
            action_network_error.postValue(true)
        }
        request(onError) {
            //
            val followListUser = Repo.http.followListUser()
            if (followListUser.isSuccess()) {
                val listUser: List<UserSearchBean> = followListUser.data!!
                mTitleList.clear()
                mTitleList.addAll(listUser)
                mCurrentSelectedUser = null
                //
            } else {
                Log.e(TAG, "result0=" + followListUser.msg)
                errorRetry = {refreshFollowContents()}
                action_network_error.postValue(true)
                return@request
            }
            //
            val followSearch = Repo.http.followSearch(1, mPageSize)
            if(followSearch.isSuccess()){
                val data:List<IndexContentBean> = followSearch.data!!
                //init
                mPageIndex = 2
                mData.clear()
                mData.addAll(data)
                if(data.size < mPageSize){
                    //没有更多数据了
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }
                showData()

            } else {
                Log.e(TAG, "result0=" + followSearch.msg)
                errorRetry = {refreshFollowContents()}
                action_network_error.postValue(true)
            }
        }
    }

    fun loadMoreFollowContents(){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            errorRetry = {loadMoreFollowContents()}
            action_network_error.postValue(true)
        }
        request(onError) {
            val result: ResultBean<List<IndexContentBean>>

            if(mCurrentSelectedUser == null){
                result = Repo.http.followSearch(mPageIndex, mPageSize)
            } else {
                result = Repo.http.getUserContent(mPageIndex, mPageSize, mCurrentSelectedUser!!.id)
            }

            if(result.isSuccess()){
                val data:List<IndexContentBean> = result.data!!
                //init
                mPageIndex++
                mData.addAll(data)
                if(data.size < mPageSize){
                    //没有更多数据了
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }
                showData()

            } else {
                Log.e(TAG, "result0=" + result.msg)
                errorRetry = {loadMoreFollowContents()}
                action_network_error.postValue(true)
            }
        }
    }

    private fun refreshFollowItemOnAll(){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            errorRetry = {refreshFollowItemOnAll()}
            action_network_error.postValue(true)
        }
        request(onError) {
            val followSearch = Repo.http.followSearch(1, mPageSize)
            if(followSearch.isSuccess()){
                val data:List<IndexContentBean> = followSearch.data!!
                //init
                mPageIndex = 2
                mData.clear()
                mData.addAll(data)
                if(data.size < mPageSize){
                    //没有更多数据了
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }
                showData()
            } else {
                Log.e(TAG, "result0=" + followSearch.msg)
                errorRetry = {refreshFollowItemOnAll()}
                action_network_error.postValue(true)
            }
        }
    }

    private fun refreshFollowItemOnUser(){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            errorRetry = {refreshFollowItemOnUser()}
            action_network_error.postValue(true)
        }
        request(onError) {
            val followSearch = Repo.http.getUserContent(1, mPageSize, mCurrentSelectedUser!!.id)
            if(followSearch.isSuccess()){
                val data:List<IndexContentBean> = followSearch.data!!
                //init
                mPageIndex = 2
                mData.clear()
                mData.addAll(data)
                if(data.size < mPageSize){
                    //没有更多数据了
                    data_enableLoadMore.postValue(false)
                } else {
                    data_enableLoadMore.postValue(true)
                }
                showData()
            } else {
                Log.e(TAG, "result0=" + followSearch.msg)
                errorRetry = {refreshFollowItemOnUser()}
                action_network_error.postValue(true)
            }
        }
    }

    fun onTitleItemClick(user: UserSearchBean){
        if(mCurrentSelectedUser != null && mCurrentSelectedUser!!.id == user.id){
            mCurrentSelectedUser = null
        } else {
            mCurrentSelectedUser = user
        }
        if(mCurrentSelectedUser == null){
            refreshFollowItemOnAll()
        } else {
            refreshFollowItemOnUser()
        }
    }

    fun onTitleChannelClick(user: UserSearchBean){
        action_toChannelPage.postValue(user)
    }

    fun retryLastCall(){
        errorRetry()
    }

}